import {Injectable} from '@angular/core';
import {AppConfig} from '../config';
import {Http, Headers} from '@angular/http';
import {DatePipe} from '@angular/common';
import {InterceptorService} from './interceptor/interceptor-service';

@Injectable()
export class TurbEarlyWarnService {
  private fomat: string = 'yyyy-MM-dd HH:mm:ss';
  private readonly param = 'reportHisWarns/getReportHisWarnVOByIds';

  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(
    private http: InterceptorService,
    private datePipe: DatePipe) {
  }

  /**
   * 根据风机、测点Ids，获得时间范围内的预警信息
   *
   * @param {number} turbineIds[]
   * @param {number} tagIds[]
   * @param {Date} start
   * @param {Date} end
   * @memberof HistoryService
   */

  public getTurbineWarns(deviceIds: number[], startTime: Date, endTime: Date, currentPage: number, pageSet: number, sortField: string, sortOrder: number, divWarnName: string): Promise<any> {
    let startFomat: string = '';
    let endFomat: string = '';

    if (deviceIds == null)
      return Promise.reject('请选择设备！');
    if (startTime >= endTime)
      return Promise.reject('开始时间应该小于结束时间！');

    startFomat = this.datePipe.transform(startTime, this.fomat);
    endFomat = this.datePipe.transform(endTime, this.fomat);
    let paging = {'currentPage': currentPage, 'pageSet': pageSet};
    let queryTerms = {'deviceIds': deviceIds, 'divWarnName': divWarnName, 'startTime': startFomat, 'endTime': endFomat};
    let sort = {'sortField': sortField, 'sortOrder': sortOrder};
    return this.http.post(this.url, JSON.stringify({'paging': paging, 'queryTerms': queryTerms, 'sort': sort}))
    // return this.http.post(this.url, JSON.stringify({ 'deviceIds': deviceIds, 'startTime': startFomat, 'endTime': endFomat,'currentPage':currentPage,'quantity':quantity,'sortField':sortField,'sortOrder':sortOrder,'divWarnName':divWarnName }))
      .toPromise()
      .then(response => {
        try {
          let result = response;
          switch (result.code) {
            case '200':
              return result;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }
      })
      .catch(this.handleError);
  }

  /**
   public getTurbineWarns(turbineIds: number[], startTime: Date, endTime: Date): Promise<TurbEarlyWarn[]> {

			let startFomat: string = "";
			let endFomat: string = "";

			if (turbineIds == null)
				return Promise.reject("请选择设备！");
			if (startTime >= endTime)
				return Promise.reject("开始时间应该小于结束时间！");

			startFomat = this.datePipe.transform(startTime, this.fomat);
			endFomat = this.datePipe.transform(endTime, this.fomat);
			return this.http.post(this.url, JSON.stringify({ 'deviceIds': turbineIds, 'startTime': startFomat, 'endTime': endFomat }))
				.toPromise()
				.then(response => {
					try {
						let result = response;
						switch (result.code) {
							case "200":
								return result.data as TurbEarlyWarn[];
							case "-1":
								throw new Error(result.message);
						}
					}
					catch (error) {
						throw error;
					}
				})
				.catch(this.handleError);
		}
   */
  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }

}


// WEBPACK FOOTER //
// ./src/app/service/turb-early-warn.service.ts
